/**
 * 
 */
package cn.ad.server.web.servlet.v2.requesthandler;

import cn.ad.server.web.servlet.v2.parser.DefaultImpressionRequestParser;
import cn.ad.server.web.servlet.v2.requests.*;
import cn.ad.server.web.spring.model.Exchange;
import cn.ad.server.web.spring.service.actions.RequestActionException;
import cn.ad.server.domain.enumration.ResponseStatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 广告展示请求处理Handler
 * @author Wu Chengjun
 */
public class ImpressionRequestHandler extends TrackingRequestHandler {
	
	private static Logger LOGGER = LoggerFactory.getLogger(ImpressionRequestHandler.class);
	
	/**
	 * 
	 */
	public ImpressionRequestHandler() {
		//设置缺省默认的展示请求解析
		setDefaultParser(new DefaultImpressionRequestParser());
	}

	/**
	 * 处理广点通夹在impression事件中的winnotic
	 * @param impRequest
	 */
	private void doGdtWinnotic(GDTImpressionRequest impRequest){
		WinoticeResponse resp = null;
		WinoticeRequest winoticeReq = null;
		try {
			winoticeReq = new WinoticeRequest();
			winoticeReq.setAdxType(Integer.valueOf(Exchange.GDT.getValue()));
			winoticeReq.setWin_price(impRequest.getWin_price());
			winoticeReq.setCid(impRequest.getCid());
			winoticeReq.setGuid(impRequest.getGuid());
			winoticeReq.setIp(impRequest.getIp());
			winoticeReq.setMid(impRequest.getMid());
			winoticeReq.setPlatform_id(impRequest.getPlatform_id());
			winoticeReq.setSid(impRequest.getSid());
			winoticeReq.setUa(impRequest.getUa());
			winoticeReq.setChannelID(impRequest.getChannelID());
			resp = (WinoticeResponse) getTrackAction().handle(winoticeReq);
		} catch (Exception e) {
			LOGGER.error("处理广点通winotice 出现异常", e);
		}finally{
			logTrackingEvent(winoticeReq, resp);
		}
	}
	
	/**
	 * 处理BES夹在impression事件中的winnotic
	 * @param impRequest
	 */
	private void doBesWinnotic(BESImpressionRequest impRequest){
		WinoticeResponse resp = null;
		WinoticeRequest winoticeReq = null;
		try {
			winoticeReq = new WinoticeRequest();
			winoticeReq.setAdxType(Integer.valueOf(Exchange.BES.getValue()));
			winoticeReq.setWin_price(impRequest.getWin_price());
			winoticeReq.setCid(impRequest.getCid());
			winoticeReq.setGuid(impRequest.getGuid());
			winoticeReq.setIp(impRequest.getIp());
			winoticeReq.setMid(impRequest.getMid());
			winoticeReq.setPlatform_id(impRequest.getPlatform_id());
			winoticeReq.setSid(impRequest.getSid());
			winoticeReq.setUa(impRequest.getUa());
			winoticeReq.setChannelID(impRequest.getChannelID());
			resp = (WinoticeResponse) getTrackAction().handle(winoticeReq);
			if(resp.getStatus()==0&&!impRequest.isOkDecrypt()){
				resp.setStatus(ResponseStatusCode.WIN_PRICE_PARSE_ERROR.getValue());
				resp.setExceptionMsg(impRequest.getEncryptWinPrice());
			}
		} catch (Exception e) {
			LOGGER.error("处理百度winotice 出现异常", e);
		}finally{
			logTrackingEvent(winoticeReq, resp);
		}
	}

	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.TrackingRequestHandler#handleTracking(cn.falconnect.adserver.adapter.TrackingRequest)
	 */
	@Override
	public TrackingResponse handleTracking(TrackingRequest request) throws RequestActionException {
		TrackingResponse resp = super.handleTracking(request);
		
		//广点通的曝光和win notice合在一个接口中处理
		if (request instanceof GDTImpressionRequest && ((GDTImpressionRequest) request).getWin_price() > 0 ) {
			doGdtWinnotic((GDTImpressionRequest)request);
		}
		
		//BES的曝光和win notice合在一个接口中处理
		if (request instanceof BESImpressionRequest) {
			doBesWinnotic((BESImpressionRequest)request);
		}
		return resp;
	}
	
}
